package com.example.jd.finance.sever.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.jd.common.DateUtils;
import com.example.jd.common.ExcelUtils;
import com.example.jd.common.InvokeResultUtil;
import com.example.jd.common.PageInfoUtil;
import com.example.jd.finance.entity.MsWechatTotalStatiscs;
import com.example.jd.finance.entity.param.DetailPageParam;
import com.example.jd.finance.entity.param.MsWechatTotalStatiscsExE;
import com.example.jd.finance.entity.vo.MsCustomerRecordsExcelVO;
import com.example.jd.finance.entity.vo.MsCustomerRecordsVO;
import com.example.jd.finance.entity.vo.MsQueryDetailCountVO;
import com.example.jd.finance.mapper.MsCustomerRecordsMapper;
import com.example.jd.finance.entity.MsCustomerRecords;
import com.example.jd.finance.mapper.MsWechatTotalStatiscsMapper;
import com.example.jd.finance.sever.MsCustomerRecordsService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-11-06
 */
@Service
public class MsCustomerRecordsServiceImpl extends ServiceImpl<MsCustomerRecordsMapper, MsCustomerRecords> implements MsCustomerRecordsService {

    @Autowired
    private MsCustomerRecordsMapper msCustomerRecordsMapper;

    @Autowired
    private MsWechatTotalStatiscsMapper msWechatTotalStatiscsMapper;

    @Override
    public MsCustomerRecords findOne() {
        return msCustomerRecordsMapper.findOne();
    }

    @Override
    public InvokeResultUtil detailPage(DetailPageParam param) {

        Page page = new Page(param.getPageIndex(), param.getPageSize());
        List<MsCustomerRecordsVO> list = msCustomerRecordsMapper.detailPage(page, param);
        if (CollectionUtils.isEmpty(list)) {
            return InvokeResultUtil.success(new PageInfoUtil<>());
        }
        PageInfoUtil<MsCustomerRecordsVO> pageN = new PageInfoUtil<>();
        pageN.setPageIndex(param.getPageIndex());
        pageN.setPageSize(param.getPageSize());
        pageN.setPagedItems(list);
        pageN.setTotalCount(msCustomerRecordsMapper.getCountTotoal(param));
        return InvokeResultUtil.success(pageN);
    }

    @SneakyThrows
    @Override
    public MsQueryDetailCountVO queryDetailCount(String enterpriseWechatCode, LocalDate now) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 10,
                10L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));
        MsQueryDetailCountVO vo = new MsQueryDetailCountVO();
        MsWechatTotalStatiscsExE e = new MsWechatTotalStatiscsExE();
        MsWechatTotalStatiscs s = new MsWechatTotalStatiscs();
        s.setEnterpriseWechatCode(enterpriseWechatCode);
        s.setCurrentDay(now);

        e.setEnterpriseWechatCode(enterpriseWechatCode);
        e.setCurrentDay(now);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        e.setCurrentDayTime(now.format(fmt));
        Future<MsWechatTotalStatiscs> appNumCurrentDay = threadPoolExecutor.submit(() ->
                msWechatTotalStatiscsMapper.queryCurrentDayRecord(s)
        );
        Future<Long> countApplyNum = threadPoolExecutor.submit(() ->
                msWechatTotalStatiscsMapper.queryCountApplyNum(e)
        );
        MsWechatTotalStatiscs t = appNumCurrentDay.get();
        if (t != null){
            vo.setApplyNumCurrentDay(t.getCurrentApplyTotal());
            vo.setApplyPassedNumCurrentDay(t.getCurrentApplySuccessTotal());
        }

        Future<Long> countApplyPassedCount = threadPoolExecutor.submit(() ->
                msWechatTotalStatiscsMapper.queryCountApplyPassedCount(e)
        );
        vo.setCountApplyPassedNum(countApplyPassedCount.get() == null ? 0 : countApplyPassedCount.get());
        vo.setCountApplyNum(countApplyNum.get() == null ? 0 : countApplyNum.get());
        return vo;
    }

    @SneakyThrows
    @Override
    public MsQueryDetailCountVO queryDetailCountByCurrentDay(String enterpriseWechatCode,String currentDay) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 10,
                10L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20));
        //当日申请数 ms_wechat_increase_customer_details count*  申请时间=今天
        MsQueryDetailCountVO vo = new MsQueryDetailCountVO();
        Future<Long> appNumCurrentDay = threadPoolExecutor.submit(() ->
                msCustomerRecordsMapper.queryApplyNumByCurrentDay(enterpriseWechatCode,currentDay)
        );
        //当日通过  ms_wechat_increase_customer_details count*  state =2
        Future<Long> applyPassedNumCurrentDay = threadPoolExecutor.submit(() ->
                msCustomerRecordsMapper.queryApplyPassedNumByCurrentDay(enterpriseWechatCode,currentDay)
        );
        //总申请数 ms_wechat_increase_customer_details count*
        Future<Long> countApplyNum = threadPoolExecutor.submit(() ->
                msCustomerRecordsMapper.queryCountApplyNum(enterpriseWechatCode)
        );
        //总申请数通过数 ms_wechat_increase_customer_details count*  state =2
        Future<Long> countApplyPassedNum = threadPoolExecutor.submit(() ->
                msCustomerRecordsMapper.queryCountApplyPassedNum(enterpriseWechatCode)
        );
        vo.setApplyNumCurrentDay(appNumCurrentDay.get());
        vo.setApplyPassedNumCurrentDay(applyPassedNumCurrentDay.get());
        vo.setCountApplyNum(countApplyNum.get());
        vo.setCountApplyPassedNum(countApplyPassedNum.get());
        return vo;
    }

    @Override
    public Map<String, Object> detailPageExcel(DetailPageParam param, HttpServletResponse response) {
        List<MsCustomerRecordsExcelVO> list = msCustomerRecordsMapper.detailExcel(param);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        //导出Excel
        ExcelUtils.writeExcel(response, list, MsCustomerRecordsExcelVO.class, DateUtils.getMinString());
        return null;
    }

}
